import java.io.File;

import utils.Logger;
import utils.Utils;
 

public class Main {

	public static void main(String[] args) {
		
		File output = new File("log/ee_std_out");
		
		killAndStart(args);
		
		while(!Utils.finished()) {
			try {
				Thread.sleep(30000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Logger.log("ee-monitor", "task not finished");
			if(output.lastModified() < System.currentTimeMillis() - 60000){
				Logger.log("ee-monitor", "process idle, RESTARTING");
				killAndStart(args);
			}
		}

	}
	
	private static void killAndStart(String[] args) {
		Logger.log("ee-monitor", "killing...");
		String pwd = Utils.runCmd("sh ./stop-ee.sh");
		Logger.log("ee-monitor", "running...");
		String cmd = "sh start-ee.sh";
		for(String arg: args) cmd += " " + arg;
		Logger.log("ee-monitor", cmd);
		Utils.runCmd(cmd);
	}

}
